﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using YT.DB;
using YT.Commons;

namespace YT.XH.NOSE
{
    public partial class frmNSMethodMtn : Form
    {

        private string mstrSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        private string mstrOldCase = string.Empty;
        private int  mintOldAlreadTimes = 0;
        private bool mblnShowAll = true;  //如果是协合医院就需要显示全部，如果是其它医院，则不能显示全部
        //所有医院开放"病史调查"下的"治疗方案选择"菜单。但菜单内容中，仅显示"药物保守治疗"，"安脱达屋尘螨皮下免疫治疗"和"局部手术治疗"3个选项，后面的"回访周期"、"次数"选项窗口均屏蔽，菜单栏中"添加回访"亦须屏蔽
        private FormStatus mfrmStatus = new FormStatus();
        private struct SickInfo
        {
            public string strSickRecID;
            public string strSickName;
            public string strMobilePhone;
        }

        public frmNSMethodMtn()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
        }

        private void frmNSMethodMtn_Load(object sender, EventArgs e)
        {
            FillCurOperation();
            FillWeekDay();
            this.Text = "治疗方案选择";
            timer1.Start();
            if (SysVar.HospitalID.ToUpper().Trim() != "115xy001".ToUpper())
            {
                mblnShowAll = false;
                
                radCase2.Visible = false ;
                radCase4.Visible = false;
                radCase6.Visible = false;

                pnlAllOther.Visible = false;
                btnNew.Visible = false;
            }
            else
            {
                pnlAllCase.Visible = true ;
                pnlAllOther.Visible = true ;
                btnNew.Visible = true;
                mblnShowAll = true;
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;

        }

        private void FillCurOperation()
        {
            string strSQL = "select * from mSickPerson where sickrecid='" + mstrSickRecID + "'";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow == null) return;

            GetRadFromOperationName(CvtFunc.ToString(drRow["CurOperation"]));


            if (radCase1.Checked) mstrOldCase = "1";
            if (radCase2.Checked) mstrOldCase = "2";
            if (radCase3.Checked) mstrOldCase = "3";
            if (radCase4.Checked) mstrOldCase = "4";
            if (radCase5.Checked) mstrOldCase = "5";
            if (radCase6.Checked) mstrOldCase = "6";
            if (mstrOldCase == "")
                mfrmStatus = FormStatus.AddNew;
            else
                mfrmStatus = FormStatus.Update;

            strSQL = "select * from NoseVisitWeek where sickrecid='" + mstrSickRecID + "'";
            drRow = mdbcon.GetDataFirstRow (strSQL );
            if ( drRow !=null )
            {
                  if (mstrOldCase == "1") mintOldAlreadTimes = CvtFunc.ToInt(drRow["AlreadyVisit1"] ) ;
                  if (mstrOldCase == "2") mintOldAlreadTimes = CvtFunc.ToInt(drRow["AlreadyVisit2"]);
                  if (mstrOldCase == "3") mintOldAlreadTimes = CvtFunc.ToInt(drRow["AlreadyVisit3"]);
                  if (mstrOldCase == "4") mintOldAlreadTimes = CvtFunc.ToInt(drRow["AlreadyVisit4"]);
                  if (mstrOldCase == "5") mintOldAlreadTimes = CvtFunc.ToInt(drRow["AlreadyVisit5"]);
                  if (mstrOldCase == "6") mintOldAlreadTimes = CvtFunc.ToInt(drRow["AlreadyVisit6"]);
            }

        }

        private void GetRadFromOperationName(string strCaseName)
        {
            if (strCaseName == radCase1.Text) radCase1.Checked = true;
            if (strCaseName == radCase2.Text) radCase2.Checked = true;
            if (strCaseName == radCase3.Text) radCase3.Checked = true;
            if (strCaseName == radCase4.Text) radCase4.Checked = true;
            if (strCaseName == radCase5.Text) radCase5.Checked = true;
            if (strCaseName == radCase6.Text) radCase6.Checked = true;
        }

        private string GetOperationNameFromRad()
        {
            if (radCase1.Checked) return radCase1.Text;
            if (radCase2.Checked) return radCase2.Text;
            if (radCase3.Checked) return radCase3.Text;
            if (radCase4.Checked) return radCase4.Text;
            if (radCase5.Checked) return radCase5.Text;
            if (radCase6.Checked) return radCase6.Text;
            return "";
        }

        private bool UpdateDataForOnlyOtherHispital()
        {
            if (GetOperationNameFromRad() == "")
            {
                MessageBox.Show("治疗方案不能为空.", SysVar.WarnTitle);
                return false;
            }

            string strSQL = string.Empty;
            string strOldCaseName = DataFunctions.GetFieldValue("mSickPerson", "CurOperation", "SickRecID='" + mstrSickRecID + "'");
            strSQL = "update mSickPerson set CurOperation='" + GetOperationNameFromRad() + "' where sickrecid='" + mstrSickRecID + "'";
            mdbcon.ExecuteSql(strSQL);
            string strTreatCaseName = GetOperationNameFromRad();
            if (strOldCaseName != strTreatCaseName)
            {
                int intPeriod = CvtFunc.ToInt(DataFunctions.GetFieldValue("mTreatHistory", "max(Period)", "SickRecID='" + mstrSickRecID + "'"));
                intPeriod++;

                strSQL = " INSERT INTO mTreatHistory (SickRecID, TreatCaseName,Period  ) "
                        + " VALUES ( '" + mstrSickRecID + "', '" + strTreatCaseName + "', '" + intPeriod + "' ) ";
                mdbcon.ExecuteSql(strSQL);
            }
            //UpdateWeekDay();

            return true; 
        }

        private bool  UpdateData()
        {
            if (GetOperationNameFromRad() == "")
            {
                MessageBox.Show("治疗方案不能为空.", SysVar.WarnTitle);
                return false;
            }
            
            string strSQL = string.Empty;
            string strOldCaseName = DataFunctions.GetFieldValue("mSickPerson", "CurOperation", "SickRecID='" + mstrSickRecID + "'");
            strSQL = "update mSickPerson set CurOperation='" + GetOperationNameFromRad() + "' where sickrecid='" + mstrSickRecID + "'";
            mdbcon.ExecuteSql(strSQL);
            string strTreatCaseName = GetOperationNameFromRad();

            if (strOldCaseName != strTreatCaseName)
            {
                int intPeriod = CvtFunc.ToInt(DataFunctions.GetFieldValue("mTreatHistory", "max(Period)", "SickRecID='" + mstrSickRecID + "'"));
                intPeriod++;

                strSQL = " INSERT INTO mTreatHistory (SickRecID, TreatCaseName,Period  ) "
                        + " VALUES ( '" + mstrSickRecID + "', '" + strTreatCaseName + "', '" + intPeriod + "' ) ";
                mdbcon.ExecuteSql(strSQL);

                // 检查是不是还有原来的发送的短信还没有完成的计划，如果有，问用户是不是要删除。
                strSQL = "select count(*) from SmsOutbox where sickrecid='" + mstrSickRecID + "' and MsgType='N' and SendSuccess=0";
                DataRow drCount = mdbcon.GetDataFirstRow(strSQL);
                if (CvtFunc.ToInt(drCount[0]) > 0)
                {
                    if (MessageBox.Show("患者还有未完成的短信回访计划，是否删除？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        strSQL = "delete SmsOutbox where sickrecid='" + mstrSickRecID + "' and MsgType='N' and SendSuccess=0";
                        mdbcon.ExecuteSql(strSQL);
                    }
                }
                UpdateMsg();
            }
            else
            {
                if (!CvtFunc.ToBoolean(DataFunctions.GetFieldValue("mSickPerson", "NeedMsg", "sickrecid='" + mstrSickRecID  + "'")))
                {
                    MessageBox.Show("不能对此患者发送短信息。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                
                string strOldCaseField = string.Empty;
                string strOldCaseWeek = string.Empty;
                string strOldCase = string.Empty;
                int intOldTimes = 0;

                TextBox txtCaseWeek = null;
                ComboBox cmbCase = null;
                ComboBox cmbTimes = null;

                bool blnCaseWeekChanged = false ;
                bool blnCaseChanged =  false ;
                bool blnTimesChanged =  false ;

                strSQL = "select * from  NoseVisitWeek where sickrecid='" + mstrSickRecID + "'";
                DataRow drOldRow = mdbcon.GetDataFirstRow(strSQL);
                if (drOldRow != null)
                {
                    if (radCase1.Checked)
                    {
                        strOldCaseWeek = CvtFunc.ToString ( drOldRow["Case1Week"]);
                        strOldCase =  CvtFunc.ToString (drOldRow["Case1"]);
                        intOldTimes = CvtFunc.ToInt(drOldRow["Times1"]);

                        txtCaseWeek = txtCase1Week;
                        cmbCase = cmbCase1;
                        cmbTimes = cmbTimes1;                                             
                    }
                    else if (radCase2.Checked)
                    {
                        strOldCaseWeek =  CvtFunc.ToString (drOldRow["Case2Week"]);
                        strOldCase = CvtFunc.ToString ( drOldRow["Case2"]);
                        intOldTimes = CvtFunc.ToInt(drOldRow["Times2"]);

                        txtCaseWeek = txtCase2Week;
                        cmbCase = cmbCase2;
                        cmbTimes = cmbTimes2;    
                    }
                    else if (radCase3.Checked)
                    {
                        strOldCaseWeek = CvtFunc.ToString ( drOldRow["Case3Week"]);
                        strOldCase = CvtFunc.ToString ( drOldRow["Case3"]);
                        intOldTimes = CvtFunc.ToInt(drOldRow["Times3"]);

                        txtCaseWeek = txtCase3Week;
                        cmbCase = cmbCase3;
                        cmbTimes = cmbTimes3;   
                    }
                    else if (radCase4.Checked)
                    {
                        strOldCaseWeek = CvtFunc.ToString ( drOldRow["Case4Week"]);
                        strOldCase = CvtFunc.ToString ( drOldRow["Case4"]);
                        intOldTimes = CvtFunc.ToInt(drOldRow["Times4"]);

                        txtCaseWeek = txtCase4Week;
                        cmbCase = cmbCase4;
                        cmbTimes = cmbTimes4;   
                    }
                    else if (radCase5.Checked)
                    {
                        strOldCaseWeek = CvtFunc.ToString ( drOldRow["Case5Week"]);
                        strOldCase =  CvtFunc.ToString (drOldRow["Case5"]);
                        intOldTimes = CvtFunc.ToInt(drOldRow["Times5"]);

                        txtCaseWeek = txtCase5Week;
                        cmbCase = cmbCase5;
                        cmbTimes = cmbTimes5;   
                    }
                    else if (radCase6.Checked)
                    {
                        strOldCaseWeek = CvtFunc.ToString ( drOldRow["Case6Week"]);
                        strOldCase = CvtFunc.ToString ( drOldRow["Case6"]);
                        intOldTimes = CvtFunc.ToInt(drOldRow["Times6"]);

                        txtCaseWeek = txtCase6Week;
                        cmbCase = cmbCase6;
                        cmbTimes = cmbTimes6;   
                    }

                    if (strOldCaseWeek.ToUpper() != txtCaseWeek.Text.ToUpper())
                    {
                        blnCaseWeekChanged = true;
                    }
                    if (strOldCase.ToUpper() != cmbCase.Text.ToUpper())
                    {
                        blnCaseChanged  = true;
                    }
                    if (intOldTimes != CvtFunc.ToInt ( cmbTimes.Text.ToUpper()))
                    {
                        blnTimesChanged   = true;
                    }

                    if (blnCaseWeekChanged || blnCaseChanged || blnTimesChanged)
                    {
                        strSQL = "delete SmsOutbox where sickrecid='" + mstrSickRecID + "' and MsgType='N' and SendSuccess=0";
                        mdbcon.ExecuteSql(strSQL);

                        UpdateMsg();
                    }
                    

                }

            }              
            UpdateWeekDay();
            
            return true;
        }

        private bool  UpdateMsg( )
        {
            bool blnNeedMsg = true ;
            if (!CvtFunc.ToBoolean(DataFunctions.GetFieldValue("mSickPerson", "NeedMsg", "sickrecid='" + mstrSickRecID + "'")))
            {
                MessageBox.Show("不能对此患者发送短信息。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                blnNeedMsg =false ;
            }
            string strWeekType = string.Empty ;
            string strCase = string.Empty;
            int intWeekTimes = 0;
            int intTotalTimes =0;

            if (radCase1.Checked)
            {
                strCase = "1";
                intTotalTimes = CvtFunc.ToInt(cmbTimes1.Text);
                strWeekType = cmbCase1.Text;
                intWeekTimes = CvtFunc.ToInt(txtCase1Week.Text);
            }
            if (radCase2.Checked)
            {
                strCase = "2";
                intTotalTimes = CvtFunc.ToInt(cmbTimes2.Text);
                strWeekType = cmbCase2.Text;
                intWeekTimes =CvtFunc.ToInt( txtCase2Week.Text);
            }
            if (radCase3.Checked) 
            {
                strCase = "3";
                intTotalTimes = CvtFunc.ToInt(cmbTimes3.Text);
                strWeekType = cmbCase3.Text;
                intWeekTimes =CvtFunc.ToInt(txtCase3Week.Text);
            }
            if (radCase4.Checked)
            {
                strCase = "4";
                intTotalTimes = CvtFunc.ToInt(cmbTimes4.Text);
                strWeekType = cmbCase4.Text ;
                intWeekTimes = CvtFunc.ToInt(txtCase4Week.Text);
            }
            if (radCase5.Checked)
            {
                strCase = "5";
                intTotalTimes = CvtFunc.ToInt(cmbTimes5.Text);
                strWeekType = cmbCase5.Text;
                intWeekTimes = CvtFunc.ToInt(txtCase5Week.Text);
            }
            if (radCase6.Checked)
            {
                strCase = "6";
                intTotalTimes = CvtFunc.ToInt(cmbTimes6.Text);
                strWeekType = cmbCase6.Text;
                intWeekTimes =CvtFunc.ToInt( txtCase6Week.Text);
            }
            int intSendTimes = 1;
            DateTime datLastSendDate = DateTime.Now.Date;
            SickInfo sickinfo = new SickInfo();
            string strSQL = "select * from mSickPerson where SickRecID='" + mstrSickRecID + "'";
            DataRow drSickInfo = mdbcon.GetDataFirstRow(strSQL);
            if (drSickInfo != null)
            {
                sickinfo.strSickName = CvtFunc.ToString(drSickInfo["SickName"]);
                sickinfo.strMobilePhone = CvtFunc.ToString(drSickInfo["MobilePhone"]);
                sickinfo.strSickRecID = mstrSickRecID;
                //for (int intRow = 1; intRow <= intTotalTimes; intRow++)
                for (int intRow = 1; intRow <= 1; intRow++)
                {
                    if (intSendTimes > intTotalTimes) break;
                    string strMsg = DataFunctions.GetFieldValue("mSmsMessage", "SmsMessage", "Type='N'");
                    int intSplitDay = 0;
                    if (CvtFunc.ToString(strWeekType) == "周")
                    {
                        int intVisit = intWeekTimes;
                        if (intVisit > 0) intSplitDay =CvtFunc.ToInt (   intVisit*7) ;

                        for (int i = 0; i < intTotalTimes; i++)
                        {
                            DateTime datSendDate = datLastSendDate.AddDays(CvtFunc.ToDouble(intSplitDay * (intSendTimes)));
                            if (intSendTimes > intTotalTimes) break;
                            if ( blnNeedMsg ) InsertMsgIntoSmsOutmsg(sickinfo, datSendDate, strMsg);
                            intSendTimes++;
                        }
                    }
                    else if (CvtFunc.ToString(strWeekType) == "月")
                    {
                        int intVisit = intWeekTimes;
                        if (intVisit > 0) intSplitDay =CvtFunc.ToInt ( intVisit);

                        for (int i = 0; i < intTotalTimes; i++)
                        {
                            DateTime datSendDate = datLastSendDate.AddMonths(CvtFunc.ToInt(intSplitDay *( intSendTimes)));
                            if (intSendTimes > intTotalTimes) break;
                            if (blnNeedMsg) InsertMsgIntoSmsOutmsg(sickinfo, datSendDate, strMsg);
                            intSendTimes++;
                        }
                    }
                    else if (CvtFunc.ToString(strWeekType) == "天")
                    {
                        int intVisit = intWeekTimes;
                        if (intVisit > 0) intSplitDay = CvtFunc.ToInt(  intVisit);

                        for (int i = 0; i < intTotalTimes; i++)
                        {
                            DateTime datSendDate = datLastSendDate.AddDays(CvtFunc.ToDouble(intSplitDay * (intSendTimes)));
                            if (intSendTimes > intTotalTimes) break;
                            if (blnNeedMsg) InsertMsgIntoSmsOutmsg(sickinfo, datSendDate, strMsg);
                            intSendTimes++;
                        }
                    }
                }
            }
            return true;

        }

        private bool InsertMsgIntoSmsOutmsg(SickInfo sickinfo, DateTime datSendDate, string strMsg)
        {
            System.Threading.Thread.Sleep(1000);
            
                string strSQL = @" Insert into SmsOutbox (SickName,SickRecID,MobilePhone,Message,SendDate,MsgType)
                                VALUES ('{0}','{1}','{2}','{3}','{4}','{5}') ";
                strSQL = string.Format(strSQL, sickinfo.strSickName
                                                , sickinfo.strSickRecID
                                                , sickinfo.strMobilePhone
                                                , strMsg
                                                , datSendDate
                                                , 'N');
                mdbcon.ExecuteSql(strSQL);
                return true;
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (mblnShowAll)
            {
                if (!UpdateData()) return;
            }
            else
            {
                if (!UpdateDataForOnlyOtherHispital()) return;
            }
            this.Close();
        }

        private void frmNSMethodMtn_FormClosed(object sender, FormClosedEventArgs e)
        {
            mdbcon.CloseCon(); 
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FillWeekDay()
        {

            string strSQL = string.Empty;
            strSQL = "select  * from NoseVisitWeek where  SickRecId='" + mstrSickRecID + "' ";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                txtCase1Week.Text =CvtFunc.ToString ( drRow["Case1Week"]);
                cmbCase1.Text = CvtFunc.ToString (drRow["Case1"]);
                cmbTimes1.Text = CvtFunc.ToString (drRow["Times1"]);
                txtCase2Week.Text = CvtFunc.ToString (drRow["Case2Week"]);
                cmbCase2.Text = CvtFunc.ToString (drRow["Case2"]);
                cmbTimes2.Text = CvtFunc.ToString (drRow["Times2"]);
                txtCase3Week.Text = CvtFunc.ToString (drRow["Case3Week"]);
                cmbCase3.Text = CvtFunc.ToString (drRow["Case3"]);
                cmbTimes3.Text = CvtFunc.ToString (drRow["Times3"]);
                txtCase4Week.Text = CvtFunc.ToString (drRow["Case4Week"]);
                cmbCase4.Text = CvtFunc.ToString (drRow["Case4"]);
                cmbTimes4.Text = CvtFunc.ToString (drRow["Times4"]);
                txtCase5Week.Text = CvtFunc.ToString (drRow["Case5Week"]);
                cmbCase5.Text = CvtFunc.ToString (drRow["Case5"]);
                cmbTimes5.Text = CvtFunc.ToString (drRow["Times5"]);
                
                txtCase6Week.Text = CvtFunc.ToString(drRow["Case6Week"]);
                cmbCase6.Text = CvtFunc.ToString(drRow["Case6"]);
                cmbTimes6.Text = CvtFunc.ToString(drRow["Times6"]);
            }
        }

        private void UpdateWeekDay()
        {
            string strSQL = string.Empty;

            strSQL = " Delete NoseVisitWeek where  SickRecId='" + mstrSickRecID + "' ";
            mdbcon.ExecuteSql(strSQL);

            strSQL = " INSERT INTO NoseVisitWeek ( SickRecID, Case1Week,Case1,Times1, " +                      
                      " Case2Week,Case2,Times2, " +
                      " Case3Week,Case3,Times3, " +
                      " Case4Week,Case4,Times4, " +
                      " Case5Week,Case5,Times5, " +
                      " Case6Week,Case6,Times6, " +
                      " TreatCaseName1,TreatCaseName2,TreatCaseName3,TreatCaseName4,TreatCaseName5,TreatCaseName6 " +
                        " ) " +
                      " VALUES ( " +
                      " '{0}', "  + 
                      " '{1}','{2}','{3}', " +   
                      " '{4}','{5}','{6}', "+
                      " '{7}','{8}','{9}', " +
                      " '{10}','{11}','{12}', " +
                      " '{13}','{14}','{15}', " +
                      " '{16}','{17}','{18}', " +
                      " '{19}','{20}','{21}','{22}','{23}','{24}' " +
                      " )";
            string[] strSQLPara =new string[]{ mstrSickRecID , 
                        txtCase1Week.Text , cmbCase1.Text  , cmbTimes1.Text,
                        txtCase2Week.Text , cmbCase2.Text  , cmbTimes2.Text,
                        txtCase3Week.Text , cmbCase3.Text  , cmbTimes3.Text,
                        txtCase4Week.Text , cmbCase4.Text  , cmbTimes4.Text,
                        txtCase5Week.Text , cmbCase5.Text  , cmbTimes5.Text,
                        txtCase6Week.Text , cmbCase6.Text  , cmbTimes6.Text,
                        radCase1.Text ,radCase2.Text ,radCase3.Text ,radCase4.Text ,radCase5.Text,radCase6.Text 
                        }; 
                        
            strSQL =string.Format(strSQL , strSQLPara );

            mdbcon.ExecuteSql(strSQL);


            int intRow = 0;
            if (radCase1.Checked) intRow = 1;
            if (radCase2.Checked) intRow = 2;
            if (radCase3.Checked) intRow = 3;
            if (radCase4.Checked) intRow = 4;
            if (radCase5.Checked) intRow = 5;
            if (radCase6.Checked) intRow = 6;
            strSQL ="update NoseVisitWeek set CurCaseSeqNo= '{0}' where sickRecId='"+ mstrSickRecID +"'";
            strSQL = string.Format(strSQL, intRow);
            mdbcon.ExecuteSql(strSQL);

            if (intRow.ToString() == mstrOldCase)
            {
                strSQL = "update NoseVisitWeek set AlreadyVisit" + mstrOldCase + " =" + mintOldAlreadTimes + " where SickRecID='" + mstrSickRecID + "'  ";
                mdbcon.ExecuteSql(strSQL);
            }
            
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            UpdateData();
            string strCaseName = string.Empty;
            RadioButton radCase = null;
            if (radCase1.Checked) radCase = radCase1;
            if (radCase2.Checked) radCase = radCase2; ;
            if (radCase3.Checked) radCase = radCase3; ;
            if (radCase4.Checked) radCase = radCase4; ;
            if (radCase5.Checked) radCase = radCase5; ;
            if (radCase6.Checked) radCase = radCase6; ;
            strCaseName = radCase.Text;
            frmNSMethodMtn_Add frmAdd = new frmNSMethodMtn_Add(mstrSickRecID, strCaseName);
            frmAdd.ShowDialog(); 
        }

      

    }
}


